#include<bits/stdc++.h>
#define ONLINE_JUDGE
#define ll long long
#define ull unsigned long long
using namespace std;

const int N = 1e4 + 10, INF = 0x3f3f3f3f;

int n, m;
stack<int> st;
int h[N], e[N * 5], ne[N * 5], idx = 2;
int scc[N], dfn[N], low[N], cnt, tims;
int out[N], scc_cnt[N];
void add(int a, int b) { e[idx] = b; ne[idx] = h[a]; h[a] = idx++; }

void tarjan(int x)
{
    st.push(x);
    dfn[x] = low[x] = ++tims;
    for (int i = h[x];i;i = ne[i]) {
        int y = e[i];
        if (dfn[y] == 0) {
            tarjan(y);
            low[x] = min(low[x], low[y]);
        } else if (scc[y] == 0) low[x] = min(low[x], dfn[y]);
    }
    if (dfn[x] == low[x]) {
        cnt++;
        while (1) {
            int y = st.top();st.pop();
            scc[y] = cnt;
            scc_cnt[cnt]++;
            if (x == y) break;
        }
    }
}

void solve()
{
    cin >> n >> m;
    for (int i = 0, x, y;i < m;i++) {
        cin >> x >> y;
        add(x, y);
    }
    for (int i = 1;i <= n;i++) if (dfn[i] == 0) tarjan(i);

    for (int x = 1;x <= n;x++)
        for (int i = h[x];i;i = ne[i]) {
            int y = e[i];
            if (scc[x] != scc[y])out[scc[x]]++;
        }

    int t = 0, p;
    for (int i = 1;i <= cnt;i++) if (out[i] == 0) t++, p = i;
    if (t == 1) cout << scc_cnt[p] << '\n';
    else cout << 0 << '\n';
}

signed main()
{
    // ios::sync_with_stdio(0);
    // cin.tie(0);cout.tie(0);
    int T = 1;
    //cin>>T;
    for (int i = 1;i <= T;i++) {
        solve();
    }
}